package com.nwpu.coursesupport.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nwpu.coursesupport.criteria.LessonResourceSearchCriteria;
import com.nwpu.coursesupport.entity.bean.JSONResult;
import com.nwpu.coursesupport.exception.BatchResourceAssignmentException;
import com.nwpu.coursesupport.service.LessonResourceService;
import com.nwpu.coursesupport.service.param.IDParam;
import com.nwpu.coursesupport.service.param.LessonResourceServiceParam;
import com.nwpu.coursesupport.tool.JSONUtils;

@Controller
@RequestMapping("/lessonResource")
public class LessonResourceController extends AbstractController{

	@Autowired
	private LessonResourceService lessonResourceService;
	
	
	@RequestMapping("/assignLesson")
	@ResponseBody
	public JSONResult assignLesson(@RequestParam("data")String data){
		JSONResult rst=new JSONResult();
		rst.fail();
		LessonResourceServiceParam.UnAssignLessonParam p=JSONUtils.json2pojo(data, LessonResourceServiceParam.UnAssignLessonParam.class);
		if(p==null){
			return rst.fail("参数转化错误");
		}
		try{
			rst=this.lessonResourceService.getUnAssignLesson(p);
		}catch(Exception e){
			return rst.fail("后台错误");
		}
		return rst;
	}
	
	
	@RequestMapping("/ajaxListData")
	@ResponseBody
	public JSONResult ajaxListData(@RequestParam("id")int id){
		JSONResult rst=new JSONResult();
		rst.fail();
		
		IDParam p=new IDParam();
		p.setId(id);
		try{
			rst=lessonResourceService.assignRecord(p);
		}catch(Exception e){
			return rst.fail("后台出错");
		}
		return rst;
	}
	
	@RequestMapping("/listData")
	public String listData(@RequestParam("id")int id,HttpServletRequest request){
		JSONResult rst=new JSONResult();
		rst.fail();
		IDParam p=new IDParam();
		p.setId(id);
		try{
			rst=lessonResourceService.assignRecord(p);
			request.setAttribute("data", this.parseData(rst));
		}catch(Exception e){
			rst.fail("后台出错");
			request.setAttribute("data", this.parseData(rst));
		}
		return "/teapage/courseResourcePublish";
	}
	
	@RequestMapping("/ajaxBatchCreate")
	@ResponseBody
	public JSONResult batchCreate(@RequestParam("data")String data){
		JSONResult rst=new JSONResult();
		rst.fail();
		LessonResourceServiceParam.BatchCreateParam p=JSONUtils.json2pojo(data, LessonResourceServiceParam.BatchCreateParam.class);
		if(p==null){
			return rst.fail("参数转换错误");
		}
		try{
			rst=this.lessonResourceService.batchCreate(p);
		}catch(BatchResourceAssignmentException e){
			return rst.fail("批量分配失败");
		}catch(Exception e){
			return rst.fail("后台错误");
		}
		return rst;
	}
	
	@RequestMapping("/ajaxDelete")
	@ResponseBody
	public JSONResult ajaxDelete(@RequestParam("id")int id){
		JSONResult rst=new JSONResult();
		rst.fail();
		IDParam p=new IDParam();
		p.setId(id);
		try{
			rst=lessonResourceService.remove(p);
		}catch(Exception e){
			return rst.fail("后台出错");
		}
		return rst;
	}
	
	@RequestMapping("/ajaxListResource")
	@ResponseBody
	public JSONResult ajaxSListResource(@RequestParam("data")String data){
		JSONResult rst=new JSONResult();
		rst.fail();
		LessonResourceSearchCriteria sc=JSONUtils.json2pojo(data, LessonResourceSearchCriteria.class);
		if(sc==null){
			return rst.fail("参数转换错误");
		}
		try{
			rst=this.lessonResourceService.assignRecordsInLessonTView(sc);
		}catch(Exception e){
			return rst.fail("后台出错");
		}
		return rst;
	}
	
	@RequestMapping("/listSResource")
	public String ajaxSListResource(@RequestParam("lessonId")int id,HttpServletRequest request){
		JSONResult rst=new JSONResult();
		rst.fail();
		LessonResourceSearchCriteria sc=new LessonResourceSearchCriteria();
		sc.setLessonId(id);
		try{
			rst=this.lessonResourceService.assignRecordsInLessonSView(sc);
		}catch(Exception e){
			 rst.fail("后台出错");
		}
		request.setAttribute("data", this.parseData(rst));
		return "/stupage/lessonResource";
	}
	
	@RequestMapping("/listTResource")
	public String ajaxTListResource(@RequestParam("lessonId")int id,HttpServletRequest request){
		JSONResult rst=new JSONResult();
		rst.fail();
		LessonResourceSearchCriteria sc=new LessonResourceSearchCriteria();
		sc.setLessonId(id);
		try{
			rst=this.lessonResourceService.assignRecordsInLessonTView(sc);
		}catch(Exception e){
			 rst.fail("后台出错");
		}
		request.setAttribute("data", this.parseData(rst));
		return "/teapage/classroomResource";
	}
}
